<?php
/**
 * Created by PhpStorm.
 * User: 聂尧
 * Date: 2017/7/20
 * Time: 11:54
 * function:菜单管理类
 */
namespace app\admin\controller;
use       think\Controller;
use       think\Request;
use       think\Db;


class Menu extends Controller
{
    //菜单列表
    public function index()
    {
        //得到一级菜单
        $list=Db::query('select *from x_system_menu where pid=:pid',['pid'=>0]);
        $this->assign('list',$list);
        return $this->fetch();
    }

    //添加菜单选项 html
    public function menu_add()
    {
        $count=Db::name('x_system_menu')->count();
        $this->assign('count',$count);
        return $this->fetch();
    }

    //添加一级菜单
    public function menu_add_f()
    {
        $data=$_POST;

        //随机产生一个一级菜单的id
        $offest=Db::name('x_system_menu')->insert($data);
        if($offest)
        {
            return json(['code'=>1,'msg'=>'添加成功']);
        }
        return json(['code'=>0,'msg'=>'添加失败请重新添加']);
    }

    //查看二级菜单
    public function menu_two_list()
    {
        $pid  = Request::instance()->param('pid');
        $count = Db::name('x_system_menu')->where('pid',$pid)->count();
        $list  = Db::query('select * from x_system_menu where pid=:pid',['pid'=>$pid]);

        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->assign('pid',$pid);
        return $this->fetch();
    }
    public function menu_two_add()
    {
        $pid  = Request::instance()->param('pid');
        //得到一级菜单的信息
        $first =Db::name('x_system_menu')->where('m_id',$pid)->find();
        $this->assign('parent',$first);
        return $this->fetch();
    }
    //添加二级菜单
    public function menu_add_t()
    {
        $data = Request::instance()->param();
        $offest=Db::name('x_system_menu')->insert($data);
        if($offest)
        {
            return json(['code'=>1,'msg'=>'添加成功']);
        }
        return json(['code'=>0,'msg'=>'添加失败请重新添加']);
    }

    //菜单更新
    public function menu_update()
    {
        $m_id=Request::instance()->get('m_id');
        $list=Db::name('x_system_menu')->where('m_id',$m_id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //更新菜单二级
    public function menu_two_update()
    {
        $m_id=Request::instance()->get('m_id');
        $list=Db::name('x_system_menu')->where('m_id',$m_id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //更新菜单数据一级
    public function menu_update_all()
    {
        $m_id=Request::instance()->get('m_id');
        $data=Request::instance()->param();

        //随机产生一个一级菜单的id
        $offest=Db::name('x_system_menu')->where('m_id',$m_id)->update($data);
        if($offest)
        {
            return json(['code'=>1,'msg'=>'更新成功']);
        }
        return json(['code'=>0,'msg'=>'更新失败请重新添加']);
    }
    //更新菜单二级

    //删除菜单
    public function delete_menu()
    {
        $m_id=Request::instance()->get('m_id');
        $offest=Db::name('x_system_menu')->where('m_id',$m_id)->delete();
        if($offest)
        {
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败请重新删除']);
    }
}